What is bip39?
The bip39 npm package is a JavaScript implementation of the BIP39 standard for generating mnemonic codes for generating deterministic keys. It is commonly used in cryptocurrency applications for creating and managing mnemonic phrases, which can be used to derive cryptographic keys.
What are bip39's main functionalities?
Generate Mnemonic
This feature allows you to generate a random mnemonic phrase. The mnemonic phrase is a set of words that can be used to generate a deterministic wallet.
const bip39 = require('bip39');
const mnemonic = bip39.generateMnemonic();
console.log(mnemonic);
Validate Mnemonic
This feature allows you to validate a given mnemonic phrase. It checks if the mnemonic is valid according to the BIP39 standard.
const bip39 = require('bip39');
const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost';
const isValid = bip39.validateMnemonic(mnemonic);
console.log(isValid);
Mnemonic to Seed
This feature converts a mnemonic phrase to a seed. The seed can be used to derive cryptographic keys.
const bip39 = require('bip39');
const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost';
const seed = bip39.mnemonicToSeedSync(mnemonic).toString('hex');
console.log(seed);
Mnemonic to Seed with Password
This feature converts a mnemonic phrase to a seed using an optional password. The password adds an extra layer of security.
const bip39 = require('bip39');
const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost';
const seed = bip39.mnemonicToSeedSync(mnemonic, 'password').toString('hex');
console.log(seed);
Other packages similar to bip39
ethers
The ethers.js library is a complete and compact library for interacting with the Ethereum blockchain and its ecosystem. It includes functionality for generating mnemonic phrases and deriving keys, similar to bip39, but also provides a wide range of other features for interacting with Ethereum smart contracts, wallets, and more.
hdkey
The hdkey package is a library for working with hierarchical deterministic (HD) wallets. It allows for the generation and management of HD keys, which can be derived from a mnemonic phrase. While it does not generate mnemonic phrases itself, it can be used in conjunction with bip39 or similar packages to manage HD wallets.
bitcoinjs-lib
The bitcoinjs-lib library is a comprehensive library for Bitcoin-related operations. It includes functionality for generating and validating mnemonic phrases, as well as creating and managing Bitcoin transactions and addresses. It is more specialized for Bitcoin compared to bip39, which is more general-purpose.
BIP39
JavaScript implementation of Bitcoin BIP39: Mnemonic code for generating deterministic keys
Reminder for developers
Please remember to allow recovery from mnemonic phrases that have invalid checksums (or that you don't have the wordlist)
When a checksum is invalid, warn the user that the phrase is not something generated by your app, and ask if they would like to use it anyway. This way, your app only needs to hold the wordlists for your supported languages, but you can recover phrases made by other apps in other languages.
However, there should be other checks in place, such as checking to make sure the user is inputting 12 words or more separated by a space. ie. phrase.trim().split(/\s+/g).length >= 12
Removing wordlists from webpack/browserify
Browserify/Webpack bundles can get very large if you include all the wordlists, so you can now exclude wordlists to make your bundle lighter.
For example, if we want to exclude all wordlists besides chinese_simplified, you could build using the browserify command below.
$ browserify -r bip39 -s bip39 \
--exclude=./wordlists/english.json \
--exclude=./wordlists/japanese.json \
--exclude=./wordlists/spanish.json \
--exclude=./wordlists/italian.json \
--exclude=./wordlists/french.json \
--exclude=./wordlists/korean.json \
--exclude=./wordlists/czech.json \
--exclude=./wordlists/portuguese.json \
--exclude=./wordlists/chinese_traditional.json \
> bip39.browser.js
This will create a bundle that only contains the chinese_simplified wordlist, and it will be the default wordlist for all calls without explicit wordlists.
You can also do this in Webpack 5 using the IgnorePlugin
. Here is an example of excluding all non-English wordlists
...
plugins: [
new webpack.IgnorePlugin({
checkResource(resource) {
return /.*\/wordlists\/(?!english).*\.json/.test(resource)
}
}),
],
...
This is how it will look in the browser console.
> bip39.entropyToMnemonic('00000000000000000000000000000000')
"的 的 的 的 的 的 的 的 的 的 的 在"
> bip39.wordlists.chinese_simplified
Array(2048) [ "的", "一", "是", "在", "不", "了", "有", "和", "人", "这", … ]
> bip39.wordlists.english
undefined
> bip39.wordlists.japanese
undefined
> bip39.wordlists.spanish
undefined
> bip39.wordlists.italian
undefined
> bip39.wordlists.french
undefined
> bip39.wordlists.korean
undefined
> bip39.wordlists.czech
undefined
> bip39.wordlists.portuguese
undefined
> bip39.wordlists.chinese_traditional
undefined
For a list of supported wordlists check the wordlists folder. The name of the json file (minus the extension) is the name of the key to access the wordlist.
You can also change the default wordlist at runtime if you dislike the wordlist you were given as default.
> bip39.entropyToMnemonic('00000000000000000000000000000fff')
"あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あまい ろんり"
> bip39.setDefaultWordlist('italian')
undefined
> bip39.entropyToMnemonic('00000000000000000000000000000fff')
"abaco abaco abaco abaco abaco abaco abaco abaco abaco abaco aforisma zibetto"
Installation
npm install bip39
Examples
const mnemonic = bip39.generateMnemonic()
bip39.mnemonicToSeedSync('basket actual').toString('hex')
bip39.mnemonicToSeedSync('basket actual')
bip39.mnemonicToSeed('basket actual').then(console.log)
bip39.mnemonicToSeed('basket actual').then(bytes => bytes.toString('hex')).then(console.log)
bip39.mnemonicToSeedSync('basket actual', 'a password')
bip39.validateMnemonic(mnemonic)
bip39.validateMnemonic('basket actual')
const bip39 = require('bip39')
const mnemonic = bip39.entropyToMnemonic('00000000000000000000000000000000')
bip39.mnemonicToEntropy(mnemonic)